/*
 * @Author: PoJun
 * @Date: 2024-06-20 15:39:41
 * @LastEditors: PoJun
 * @LastEditTime: 2024-06-20 15:42:54
 * @Message: Nothing
 */
import { mapState } from "vuex"
import store from "@/store"

// 尝试将用户在根目录中的store/index.js的vuex的state变量，全部加载到全局变量中
let $uStoreKey = []
try {
  $uStoreKey = store.state ? Object.keys(store.state) : []
} catch (e) {
  console.log(e)
}

const mixin = {
  onLoad() {
    if (!this.$uv.vuex) {
      this.dispatchVuexTo$uv()
    }
  },
  created() {
    if (!this.$uv.vuex) {
      this.dispatchVuexTo$uv()
    }
  },
  computed: {
    // 将vuex的state中的所有变量，解构到全局混入的mixin中
    ...mapState($uStoreKey)
  },
  methods: {
    // 使用方法为：如果要修改vuex的state中的a.b变量为"c" => this.$uv.vuex('a.b', 'c')
    dispatchVuexTo$uv() {
      this.$uv.vuex = (name, value) => {
        this.$store.commit("$uStore", {
          name,
          value
        })
      }
    }
  }
}

export default mixin
